package com.clip.appaction;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.clip.entity.ClipEvent;
import com.clip.entity.Clipmessage;
import com.clip.entity.MsgNotice;
import com.clip.entity.SystemMessage;
import com.clip.entity.User;
import com.clip.service.ContactService;
import com.clip.service.EventService;
import com.clip.service.MessageService;
import com.clip.service.UserService;
import com.clip.util.Struts2Utils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@Scope("prototype")
public class MessageAppAction {
	
	@Autowired
	private MessageService messageService;
	@Autowired
	private UserService userService;
	@Autowired
	private EventService eventService; 
	@Autowired
	private ContactService contactService;
	
	private int SUCCESS=10000;
	private int FAILED=10001;
	private int PARAMERROR=10011;
	
	private int COMMENTSPAGELENGTH=20;
	private int INVITESPAGELENGTH=20;
	private int SYSTEMPAGELENGTH=20;
	
	public void getContactRequests(){
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()==0){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
		}else{
			List list=messageService.getContactRequests(currentUser);
			returnObj.put("result", SUCCESS);
			JSONArray array=new JSONArray();
			for (Object object : list) {
				User tmp=(User) object;
				JSONObject tempobj=new JSONObject();
				tempobj.put("contactname", tmp.getName());
				tempobj.put("sex", tmp.getSex());
				tempobj.put("qq", tmp.getQq()!=null&&tmp.getQq().length()>0?tmp.getQq():"");
				tempobj.put("contactemail", tmp.getEmail());
				tempobj.put("contactuid", tmp.getId());
				List templist=this.userService.getUserLogoByUid(tmp.getId());
				tempobj.put("smalllogo",templist.get(1));
				tempobj.put("biglogo", templist.get(0));
				tempobj.put("birth", tmp.getBirth());
				array.add(tempobj);
			}
			returnObj.put("requests", array);
		}
		Struts2Utils.renderJson(returnObj.toString());
	}
	
	public void getAlertEvents(){
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
		}else{
			returnObj.put("result", SUCCESS);
			JSONArray array=new JSONArray();
			Calendar now=Calendar.getInstance();
			List list=this.eventService.getAlertEventsTilnow(now, currentUser);
			for (Object object : list) {
				ClipEvent clipEvent=(ClipEvent) object;
				ClipEvent toJSONEvent=null;
				if(clipEvent.getReliedclip()>0){
					toJSONEvent=this.eventService.getClipEventById(clipEvent.getReliedclip());
				}else{
					toJSONEvent=clipEvent;
				}
				int eventid=toJSONEvent.getId();
				String title=toJSONEvent.getTitle();
				long clocktime=toJSONEvent.getAlerttime();
				JSONObject temp=new JSONObject();
				temp.put("eventid", eventid);
				temp.put("title", title);
				temp.put("starttime", toJSONEvent.getBegintime());
				temp.put("endtime", toJSONEvent.getEndtime());
				temp.put("iswholeday", toJSONEvent.getIswholeday());
				temp.put("clocktime", clocktime);
				temp.put("unameid", toJSONEvent.getUid());
				temp.put("uname", this.contactService.getUserName(currentUser.getId(),toJSONEvent.getUid()));
				List urllist=this.userService.getUserLogoByUid(toJSONEvent.getUid());
				temp.put("unamebiglogo", urllist.get(0));
				temp.put("unamesmalllogo", urllist.get(1));
				temp.put("eventtype", toJSONEvent.getEventtype());
				array.add(temp);
			}
			returnObj.put("alertevents",array);
		}
		Struts2Utils.renderJson(returnObj.toString());
	}
	
	public void loadClipmessages(){
		if(uid==0||needpagenum<0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		int perpagelength=0;
		if(pagelength!=null&&pagelength>0){
			perpagelength=pagelength;
		}else{
			perpagelength=COMMENTSPAGELENGTH;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
		}else{
			returnObj.put("result", SUCCESS);
			long totalcount=this.messageService.countNewComment(currentUser);
			BigDecimal br = new BigDecimal(totalcount).divide(new BigDecimal(perpagelength),BigDecimal.ROUND_UP);
			Integer pageCount = (br.doubleValue()<1&&br.doubleValue()>=0 ? 1 : br.intValue());
			if(needpagenum>=pageCount-1){
				returnObj.put("hasmore", 0);
			}else{
				returnObj.put("hasmore", 1);
			}
			JSONArray array=new JSONArray();
			List list=null;
			list=messageService.getNewComment(currentUser,needpagenum,perpagelength);
			for (Object object : list) {
				JSONObject temp=new JSONObject();
				MsgNotice msgNotice=(MsgNotice) object;
				ClipEvent clipEvent=eventService.getClipEventById(msgNotice.getEventid());
				if(clipEvent==null){
					this.messageService.deleteMsgNotice(msgNotice);
					continue;
				}
				Clipmessage clipmessage=messageService.findBymessageid(msgNotice.getMsgid());
				if(clipmessage==null){
					this.messageService.deleteMsgNotice(msgNotice);
					continue;
				}
				int commentuid=clipmessage.getUid();
				String name=this.contactService.getUserName(currentUser.getId(), commentuid);
				if(name==null){
					this.messageService.deleteMsgNotice(msgNotice);
					continue;
				}
				temp.put("msgid", clipmessage.getId());
				temp.put("eventid",msgNotice.getEventid());
				temp.put("title",clipEvent.getTitle());
				temp.put("uid",commentuid);
				temp.put("name",name);
				List templist=this.userService.getUserLogoByUid(commentuid);
				temp.put("smalllogo", templist.get(1));
				temp.put("biglogo", templist.get(0));
				temp.put("commentcontent", clipmessage.getContent());
				temp.put("commenttype", clipmessage.getMsgtype());
				temp.put("commenttime", clipmessage.getReplytime());
				array.add(temp);
			}
			returnObj.put("comments",array);
		}
		Struts2Utils.renderJson(returnObj.toString());
	}
	
	public void ignoreClipMessage(){
		if(uid==0||msgid==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			boolean result=this.messageService.ignoreMessage(uid,msgid);
			if(result){
				returnObj.put("result", SUCCESS);
			}else{
				returnObj.put("result", FAILED);
				returnObj.put("errormsg", "评论无法忽略");
			}
			Struts2Utils.renderJson(returnObj.toString());
		}
	}
	
	public void getNewInvites(){
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
		}else{
			returnObj.put("result", SUCCESS);
			JSONArray array=new JSONArray();
			List list=eventService.queryClipList("invite",currentUser,needpagenum,INVITESPAGELENGTH,0l,null,null,null,false);
			for (Object object : list) {
				//{"eventid":int,"title":String,"uid":int,"name":String,"smalllogo":String,"starttime":long,"endtime":long,"iswholeday":int},
				JSONObject temp=new JSONObject();
				ClipEvent clipEvent=(ClipEvent) object;
				int eventid=clipEvent.getId();
				String title=clipEvent.getTitle();
				int uid=clipEvent.getUid();
				String name=this.contactService.getUserName(currentUser.getId(), clipEvent.getUid());
				List templist=this.userService.getUserLogoByUid(clipEvent.getUid());
				String smalllogo=(String) templist.get(1);
				long starttime=clipEvent.getBegintime();
				long endtime=clipEvent.getEndtime();
				int iswholeday=clipEvent.getIswholeday();
				temp.put("eventid", eventid);
				temp.put("title", title);
				temp.put("uid", uid);
				temp.put("name", name);
				temp.put("smalllogo", smalllogo);
				temp.put("starttime", starttime);
				temp.put("endtime", endtime);
				temp.put("iswholeday", iswholeday);
				array.add(temp);
			}
			returnObj.put("alertevents",array);
		}
		Struts2Utils.renderJson(returnObj.toString());
	}
	
	public void checkmsg(){
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			List list=this.messageService.checkmsg(Calendar.getInstance(),currentUser.getId());
			returnObj.put("result", SUCCESS);
			returnObj.put("systemmsgs", list.get(0));
			returnObj.put("comments", list.get(1));
			returnObj.put("reminds", list.get(2));
			Struts2Utils.renderJson(returnObj.toString());
		}
	}
	
	public void messageWithClipevent(){
		if(uid==0||eventid==0||(acceptstatus!=0&&acceptstatus!=1)){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			boolean resultflag=this.messageService.messageWithClipevent(currentUser.getId(),eventid,acceptstatus);
			if(resultflag){
				returnObj.put("result", SUCCESS);
			}else{
				returnObj.put("result", FAILED);
				returnObj.put("errormsg", "操作失败");
			}
			Struts2Utils.renderJson(returnObj.toString());
		}
	}
	
	public void loadSystemMessage(){
		if(uid==0||needpagenum<0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		int perpagelength=0;
		if(pagelength!=null&&pagelength>0){
			perpagelength=pagelength;
		}else{
			perpagelength=SYSTEMPAGELENGTH;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			long totalcount=this.messageService.countSystemMessage(currentUser.getId());
			BigDecimal br = new BigDecimal(totalcount).divide(new BigDecimal(perpagelength),BigDecimal.ROUND_UP);
			Integer pageCount = (br.doubleValue()<1&&br.doubleValue()>=0 ? 1 : br.intValue());
			List list=null;
			list=this.messageService.getSystemMessage(currentUser.getId(), needpagenum,perpagelength );
			returnObj.put("result", SUCCESS);
			if(needpagenum>=pageCount-1){
				returnObj.put("hasmore", 0);
			}else{
				returnObj.put("hasmore", 1);
			}
			if(list==null||list.size()==0){
				returnObj.put("systemmsgs", new JSONArray());
				Struts2Utils.renderJson(returnObj.toString());
				return;
			}else{
				JSONArray jsonArray=new JSONArray();
				for (Object object : list) {
					SystemMessage systemMessage=(SystemMessage) object;
					JSONObject temp=new JSONObject();
					String fromname=this.contactService.getUserName(currentUser.getId(), systemMessage.getFromuid());
					if(fromname==null){
						this.messageService.deleteSystemMessage(systemMessage);
						continue;
					}
					temp.put("msgid", systemMessage.getId());
					int eventid=systemMessage.getEventid();
					ClipEvent clipEvent=this.eventService.getDetailClipEvent(eventid);
					if(clipEvent==null){
						this.messageService.deleteSystemMessage(systemMessage);
						continue;
					}
					String title=clipEvent.getTitle();
					if(title==null||title.length()==0){
						temp.put("eventid", 0);
					}else{
						temp.put("eventid", eventid);
						temp.put("eventtitle", title);
					}
					temp.put("msgtype", systemMessage.getMsgtype());
					temp.put("msgtime", systemMessage.getMsgtime());
					temp.put("fromuid", systemMessage.getFromuid());
					temp.put("fromuname", fromname);
					List userlogo=this.userService.getUserLogoByUid(systemMessage.getFromuid());
					temp.put("fromsmalllogo", userlogo.get(1));
					temp.put("frombiglogo", userlogo.get(0));
					temp.put("shareid", clipEvent.getShareid());
					jsonArray.add(temp);
				}
				returnObj.put("systemmsgs",jsonArray);
				Struts2Utils.renderJson(returnObj.toString());
				return;
			}
		}
	}
	
	public void deleteSystemMessage(){
		if(uid==0||msgid==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			boolean issuccess=this.messageService.deleteSystemMessage(uid,msgid);
			if(issuccess){
				returnObj.put("result", SUCCESS);
			}else{
				returnObj.put("result", FAILED);
				returnObj.put("errormsg","通知无法删除");
			}
			Struts2Utils.renderJson(returnObj.toString());
		}
	}
	
	public void deleteRecycleClipMessage(){
		if(eventid==0||msgid==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			boolean issuccess=this.messageService.deleteRecycleClipMessage(uid,eventid);
			if(issuccess){
				returnObj.put("result", SUCCESS);
			}else{
				returnObj.put("result", FAILED);
				returnObj.put("errormsg","通知无法删除");
			}
			Struts2Utils.renderJson(returnObj.toString());
		}
	}
	
	private int uid;
	private int needpagenum=0;
	private int eventid;
	private int acceptstatus;
	private int msgid;
	private Integer pagelength;
	public int getUid() {
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}

	public int getNeedpagenum() {
		return needpagenum;
	}

	public void setNeedpagenum(int needpagenum) {
		this.needpagenum = needpagenum;
	}

	public int getEventid() {
		return eventid;
	}

	public void setEventid(int eventid) {
		this.eventid = eventid;
	}

	public int getAcceptstatus() {
		return acceptstatus;
	}

	public void setAcceptstatus(int acceptstatus) {
		this.acceptstatus = acceptstatus;
	}

	public int getMsgid() {
		return msgid;
	}

	public void setMsgid(int msgid) {
		this.msgid = msgid;
	}

	public Integer getPagelength() {
		return pagelength;
	}

	public void setPagelength(Integer pagelength) {
		this.pagelength = pagelength;
	}
	
}
